//package com.tiancheng.trade.settlement.controller.api;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.tiancheng.trade.commom.core.data.Result;
//import com.tiancheng.trade.settlement.bo.SettlementBillDataBO;
//import com.tiancheng.trade.settlement.enums.MessageTypeEnum;
//import com.tiancheng.trade.settlement.exception.BusinessException;
//import com.tiancheng.trade.settlement.exception.error.BusinessErrorInfoEnum;
//import com.tiancheng.trade.settlement.model.settlement.MessageBody;
//import com.tiancheng.trade.settlement.service.settlement.IFlowSubOrderService;
//import com.tiancheng.trade.settlement.service.settlement.IPaymentBillService;
//import com.tiancheng.trade.settlement.service.settlement.ISRevenueDetailService;
//import com.tiancheng.trade.settlement.service.settlement.ISettlementBillDataService;
//import com.tiancheng.trade.settlement.util.*;
//import com.tiancheng.trade.settlement.vo.settlement.api.SettlementBillDataVO;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.annotation.Resource;
//import java.util.*;
//
//@RestController
//@RequestMapping("/api/cmq")
//@Validated
//@Slf4j
//public class CmqMessageController {
//    @Autowired
//    private ObjectMapper objectMapper;
//    @Resource
//    private JacksonJsonUtil jacksonJsonUtil;
//    @Resource
//    private ISettlementBillDataService settlementService;
//    @Resource
//    private IFlowSubOrderService flowSubOrderService;
//    @Autowired
//    private IPaymentBillService billService;
//    @Resource
//    private SensorDataHandle sensorDataHandle;
//    @Resource
//    private ISRevenueDetailService revenueDetailService;
//    @Resource
//    private SensorChannelHandle sensorChannelHandle;
//    @Resource
//    private OrderSuccessHandle orderSuccessHandle;
//
//    //接收流水对应的子订单数据
//    @PostMapping("/message")
//    public Result saveFlowSubOrder(@RequestBody String recordResult) {
//        try {
//            Map<String, String> messageMap = jacksonJsonUtil.fromJson(recordResult, Map.class);
//            String msgBody = messageMap.get("msgBody");
//            Map<String, String> dataMap = jacksonJsonUtil.fromJson(msgBody, Map.class);
//            //取message_type先用if else 判断，设计模式改动较多。
//            String messageType = dataMap.get("message_type");
//            int count = 0;
////            if (MessageTypeEnum.STSO.getCode().equals(messageType)) {
////                log.info("接收子订单的消息 tag:{}", MessageTypeEnum.STSO.getDesc());
////                List<FlowSubOrderVO> subOrders = objectMapper.readValue(jacksonJsonUtil.toJson(dataMap.get("data")), new TypeReference<List<FlowSubOrderVO>>() {
////                });
////                count = flowSubOrderService.save(subOrders);
////            } else if (MessageTypeEnum.ST.getCode().equals(messageType)) {
////                log.info("接收核销与退款的消息 tag:{}", MessageTypeEnum.ST.getDesc());
////                List<SettlementBillDataVO> verifications = objectMapper.readValue(jacksonJsonUtil.toJson(dataMap.get("data")), new TypeReference<List<SettlementBillDataVO>>() {
////                });
////                List<SettlementBillDataBO> verificationBOs = this.toSettlementBillDataBO(verifications);
////                count = settlementService.saveBill(verificationBOs);
////            } else if (MessageTypeEnum.STF.getCode().equals(messageType)) {
////                log.info("接收流水的消息 tag:{}", MessageTypeEnum.STF.getDesc());
////                PaymentBillVO billVO = objectMapper.readValue(jacksonJsonUtil.toJson(dataMap.get("data")), new TypeReference<PaymentBillVO>() {
////                });
////                count = billService.recordBill(billVO);
////            } else
//            if (MessageTypeEnum.PAY.getCode().equals(messageType)) {
//                log.info("接收订单操作信息：{}", recordResult);
//                sensorDataHandle.pushOrderMessage(dataMap);
//                count = 1;
//            } else if(MessageTypeEnum.CHANNEL_REPORT.getCode().equals(messageType)){
//                count = 1;
//                log.info("接收广告渠道上报信息：{}", recordResult);
//                sensorChannelHandle.pushMessage(dataMap);
//            }else if (MessageTypeEnum.ORDER_SC.getCode().equals(messageType)) {
//                log.info("接收订单服务成功信息：{}", recordResult);
//                orderSuccessHandle.pushOrderMessage(dataMap);
//                count = 1;
//            } else {
//                log.info("接收未知类型信息：type：{},data:{}", messageType, recordResult);
//                count = 1;
//            }
//            if (count > 0) {
//                return Result.success();
//            } else {
//                return Result.fail("接收消息内容失败");
//            }
//        } catch (BusinessException e) {
//            log.error("消息处理异常: {}", e.getMessage(), e);
//            if (BusinessErrorInfoEnum.MESSAGE_ARGS_ERROR.getCode() == e.getErrorInfo().getCode()) {
//                return Result.success();
//            }
//            return Result.fail("消息处理异常");
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("接收cmq出现异常;", e);
//            return Result.fail("保存消息内容时出现异常" + e.getMessage());
//        }
//    }
//
//
//    //接收流水对应的子订单数据
//    @PostMapping("/test")
//    public Result test(@RequestBody String message) {
//        log.info("接收cmq消息{}", message);
//        Map<String, String> messageMap = JsonUtil.fromJson(message, Map.class);
//        MessageBody msgBody = JsonUtil.fromJson(messageMap.get("msgBody"), MessageBody.class);
//
//        // 验证签名
//        SortedMap<String, Object> sortedMap = JsonUtil.fromJson(JsonUtil.toJson(msgBody), TreeMap.class);
//        sortedMap.remove("sign");
//        Optional<String> sign = MessageSign.get(sortedMap, "123123");
//        return Result.success(sign);
//    }
//
//
//    private List<SettlementBillDataBO> toSettlementBillDataBO(List<SettlementBillDataVO> verifications) {
//        List<SettlementBillDataBO> verificationBOs = new ArrayList<SettlementBillDataBO>();
//        for (SettlementBillDataVO vo : verifications) {
//            SettlementBillDataBO orderBO = ObjectConvertUtil.convert(vo, SettlementBillDataBO.class).get();
//            verificationBOs.add(orderBO);
//
//        }
//        return verificationBOs;
//    }
//}
